package com.exercise.widgets.textview

import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.animation.AccelerateInterpolator
import android.view.animation.LinearInterpolator
import androidx.core.animation.doOnEnd
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivityTextViewBinding

class TextViewActivity : BaseActivity<BaseViewModel, ActivityTextViewBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityTextViewBinding
        get() = ActivityTextViewBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        initRotateExpandAnimator()
        binding.tv4.setOnClickListener {
            if (!mExpanded) {
                mRotateExpandAnimator?.start()
            } else {
                mRotateExpandAnimator?.reverse()
            }
        }
    }

    private var mRotateExpandAnimator: ObjectAnimator? = null
    private var mExpanded = false
    private fun initRotateExpandAnimator() {
        val drawable = binding.tv4.compoundDrawables[2] ?: binding.tv4.compoundDrawablesRelative[2]
        if (drawable == null) {
            return
        }
        val shortAnim = resources.getInteger(android.R.integer.config_shortAnimTime)
        mRotateExpandAnimator = ObjectAnimator.ofInt(
            drawable,
            "level",
            0,
            10000
        ).apply {
            duration = shortAnim.toLong()
            interpolator = AccelerateInterpolator()
            doOnEnd {
                mExpanded = !mExpanded
            }
        }


    }
}